/**
 * Copyright (C) 2016-2019 DSpot Sp. z o.o
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dspot.declex.actions;

import ohos.agp.components.DirectionalLayout;
import ohos.app.Context;

import com.dspot.declex.actions.component.ProgressDialog;
import com.dspot.declex.annotation.action.ActionFor;
import com.dspot.declex.annotation.action.Assignable;
import com.dspot.declex.annotation.action.FormattedExpression;
import com.dspot.declex.annotation.action.StopOn;

import org.ohosannotations.annotations.RootContext;

@ActionFor("ProgressDialog")
public class ProgressDialogActionHolder {

    ProgressDialog dialog;

    @RootContext
    Context context;

    private Runnable Shown;

    void init() {
        dialog = new ProgressDialog(context);
    }

    //Here you can infer any parameter, the first parameter is the next listener
    void build(
        final Runnable Shown,
        final Runnable Destroy) {

        this.Shown = Shown;

        if (Destroy != null) {
            dialog.setDestroyedListener(new ProgressDialog.DestroyedListener() {
                @Override
                public void onDestroy() {
                    Destroy.run();
                }
            });
        }
    }

    ;

    void execute() {
        dialog.show();
        if (Shown != null) Shown.run();
    }

    /**
     * @return Internal  Dialog instance.
     */
    @StopOn("show")
    public ProgressDialog dialog() {
        return this.dialog;
    }

    /**
     * Assigns the Internal  Dialog instance.
     *
     * @param dialog The variable to which the dialog is going to be assigned
     */
    public ProgressDialogActionHolder dialog(@Assignable("dialog") ProgressDialog dialog) {
        return this;
    }

    public ProgressDialogActionHolder title(int titleRes) {
        dialog.setTitleText(context.getString(titleRes));
        return this;
    }

    public ProgressDialogActionHolder title(@FormattedExpression String title) {
        dialog.setTitleText(title);
        return this;
    }

    public ProgressDialogActionHolder customTitle(DirectionalLayout customTitleView) {
        dialog.setTitleCustomComponent(customTitleView);
        return this;
    }

    public ProgressDialogActionHolder message(int messageRes) {
        dialog.setContentText(context.getString(messageRes));
        return this;
    }

    public ProgressDialogActionHolder message(@FormattedExpression String message) {
        dialog.setContentText(message);
        return this;
    }
}
